package com.example.test_event_and_callback.infras.async;

import com.example.test_event_and_callback.impl.TestAsyncDto;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;

public class MethodExchange {

    public static MethodHandle toMethodHandle(Method method) {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodType methodType = MethodType.methodType(method.getReturnType(), method.getParameterTypes());
        MethodHandle methodHandle;
        try {
            methodHandle = lookup.findVirtual(method.getDeclaringClass(), method.getName(), methodType);
        } catch (NoSuchMethodException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return methodHandle;
    }
}
